#include "MatlabTest.h"
#include "problem1.h"

#include "engine.h"

void MatlabTest::namesVector(Engine * ep, Problem * problem, Method * method)
{
    mxArray *S;
    const char *str[problem->getNumberOfProblems()];

    for(int i=0; i<problem->getNumberOfProblems(); i++)
    {
        OptimizedValue optStruct=method->optimize(i);
        str[i]=problem->getFun()[i].name.c_str();
    }
    S=mxCreateCharMatrixFromStrings(problem->getNumberOfProblems(),str);
    engPutVariable(ep,"names",S);
    mxDestroyArray(S);
}


//number passed here commes from optimize(number)
//it checks if problem instance is set correctly when algorithm runs
void MatlabTest::checkProblemIsSetRight(Engine * ep, Problem * problem, Method * method)
{
    mxArray *A;
    A=mxCreateDoubleMatrix(problem->getNumberOfProblems(),100,mxREAL);
    double * ptrA=mxGetPr(A);
    ptrA[0]=0.0;
    int i,j;
OptimizedValue optStruct;
    for(i=0; i<problem->getNumberOfProblems(); i++)
    {
        optStruct=method->optimize(i);
        for(j=0; j<100; j++)
        {
            if(j<problem->getCoordinates().size())
                ptrA[problem->getNumberOfProblems()*j+i]=problem->getCoordinates()[j].getUpperBound();
        }
    }

    engPutVariable(ep,"upper",A);
    mxDestroyArray(A);
    //problem->getFun()[number].name.c_str()
}

void MatlabTest::plot3D(Engine * ep, Problem * problem, Method * method)
{

    //the same 50*50 mesh array for all functions
    //might do ptrA[NP*i+j]s
    int num=8;
    mxArray *A;
    A=mxCreateDoubleMatrix(20,20,mxREAL);
    double * ptrA=mxGetPr(A);
    ptrA[0]=0.0;
    int i,j;
    OptimizedValue optStruct;
    for(i=0; i<20; i++)
    {
//matlab columns is population size
        for(j=0; j<20; j++){
            method->setIterations((i+1)*50);
            method->setPopulationSize((j+1)*50);
            std::cout<<method->getIterations()<<"*"<<method->getPopulationSize()<<"\n";
        optStruct=method->optimize(num);
                ptrA[20*j+i]=optStruct.value;
        }
    }
    engPutVariable(ep,problem->getFun()[num].name.c_str(),A);
    mxDestroyArray(A);
}



